﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PcArduinoControl
{
	public class DigitalInputPin :
		IDigitalPin
	{
		internal event DigitalInputStreamChangedHandler StreamModeChanged;
		public event DigitalInputChangeHandler DigitalValueChanged;
		
		public byte Number { get; protected set; }
		public PinType Type { get { return PinType.Input; } }

		public InputStreamMode StreamMode
		{
			get
			{
				return mStreamMode;
			}
			set
			{
				mStreamMode = value;
				OnStreamModeChanged();
			}
		}

		InputStreamMode mStreamMode;

		public DigitalValue DigitalValue
		{
			get
			{
				return mDigitalValue;
			}
			internal set
			{
				mDigitalValue = value;
				OnDigitalValueChanged();
			}
		}

		DigitalValue mDigitalValue;

		public DigitalInputPin(byte pin)
		{
			Number = pin;
			mStreamMode = InputStreamMode.Never;
		}

		void OnStreamModeChanged()
		{
			var valueChanged = StreamModeChanged;
			if (valueChanged != null)
				valueChanged.Invoke(Number, mStreamMode);
		}

		void OnDigitalValueChanged()
		{
			var valueChanged = DigitalValueChanged;
			if (valueChanged != null)
				valueChanged(Number, mDigitalValue);
		}
	}
}
